<?php
session_start();
include_once("../models/webconfig.php");
include_once("../models/class.connection.php");
include_once("../models/entities/class.ProductEntity.php");
include_once("../models/entities/class.CommentEntity.php");
include_once("../models/repositories/class.ProductRepository.php");
include_once("../models/entities/class.SectionEntity.php");
include_once("../models/entities/class.CategoryEntity.php");
include_once("../models/repositories/class.SectionRepository.php");
include_once("../models/repositories/class.CategoryRepository.php");
include("../models/entities/class.AccountEntity.php");
include("../models/repositories/class.AccountRepository.php");
if (isset($_GET["id"])) {
    $productID = $_GET["id"];
    $repository = new ProductRepository();
    $product = $repository->GetDetail($productID);
    $commentList = $repository->GetCommentsByProductID($productID);
    $imageList = explode("|", $product->ImageSrc_s);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
        <title>Chi tiết sản phẩm</title>
        <link rel="stylesheet" type="text/css" href="../content/front-end.css" />
        <link rel="stylesheet" type="text/css" href="../content/detail.css"/>
        <script type="text/javascript" src="../scripts/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="../scripts/detail.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                MainScript.init();
            })
        </script>
    </head>

    <body>
        <div id="wrap">
            <?php
            include("../includes/frontend_Header.php");
            include("../includes/frontend_Left.php");
            ?>
            <div id="inside" style="background-color: white; float:right;max-width:720px; width:720px; text-align:center;">
                <form method="post" action="">
                    <?php if (isset($product)) {
                    ?>
                    <?php if ($product->Code_s != "") {
 ?>
                            <div id="productInfo" style="margin-top:20px; margin-bottom:20px;">
                                <h3>Mã sản phẩm: <span style="color:orangered;"><?php echo $product->Code_s; ?></span> - Tên sản phẩm: <span style="color:orangered;"><?php echo $product->Name_s; ?></span></h3>
                                <input type="hidden" name="productId" id="productId" value="<?php echo $product->Code_s; ?>"/>
                            </div>
                            <center>
                                <div id="viewImage" style="text-align: center;width: 400px;">
                                    <div style="max-width: 400px;max-height: 400px;">
                                <?php if (isset($imageList)) {
                                ?>
<?php if (count($imageList) > 0) { ?>
                                        <img src="../content/images/Products/<?php echo $product->Code_s; ?>/<?php echo $imageList[0]; ?>" name="mainImage" id="mainImage" style="max-width: 400px; max-height: 400px;" alt="" />
<?php } else { ?>
                                        <img src="../content/images/no_image.jpg" name="mainImage" id="mainImage" style="max-width: 400px; max-height: 400px;" alt="" />
                                <?php } ?>
<?php } else { ?>
                                    <img src="../content/images/no_image.jpg" name="mainImage" id="mainImage" style="max-width: 400px; max-height: 400px;" alt="" />
<?php } ?>
                            </div>
                        </div>
                    </center>

<?php if (count($imageList) > 0) { ?>
                                    <div id="thumbnail">
                                        <center>
                                            <table style="width:600px;">
                                <?php
                                    for ($index = 0; $index < count($imageList); $index = $index + 4) {
                                        if (isset($imageList[$index])) {
                                ?>
                                            <tr>
                                                <td style="padding-bottom:10px;padding-top:10px;width:150px;">
                                        <?php
                                            $i = 0;
                                            while ($i < 4) {
                                                if (isset($imageList[$i])) {
                                        ?>
                                                    <div style="width: 120px;height: 120px;border: 1px solid #4d4b49; padding: 5px;text-align: center;vertical-align:middle;float:left;margin-left:5px;">
                                                        <img name="first" class="imgItem" style="cursor: pointer;" src="../content/images/Products/<?php echo $product->Code_s; ?>/<?php echo $imageList[$i]; ?>" alt="Xem hình"/></div>
                                        <?php
                                                    $i++;
                                                } else {
                                                    break;
                                                }
                                            }
                                        ?>
                                        </td>
                                    </tr>
                                <?php
                                        } else
                                            break;
                                    }
                                ?>
                                </table>
                            </center>
                        </div>
<?php } ?>
                                <div style="overflow: hidden;clear: both;margin: 10px 0px 10px 0px;text-align: left;padding-left:30px;">
                                    <p><input name="price" id="price" type="hidden" value="giaKM" />
                                        <span style="font-size: 13px;">Giá:</span>
                                        <span style="font-size: 16px;line-height: 20px;font-weight:bold;color: darkorange;"><?php echo(number_format($product->Price_f, 0)); ?> VND</span>
                                    </p>
                                    <p><span style="font-size: 13px;">Màu sắc: </span><?php echo $product->Color_s; ?></p>
                                    <img style="cursor: pointer" id="buyNow" alt="Thêm sản phẩm vào giỏ hàng" src="../content/images/buyNow_Vi.png"/>
                                </div>
                                <div id="divMessage" style="display: none;">
                                    <p id="message" style="margin: 7px 0px 8px 0px; color: red; font-weight: bold;"></p>
                                </div>
                                <div id="cmt">
                                    <div id="tabInfo">
                                        <ul class="css-tabs">
                                            <li><a class="current" href="#" onclick="">Thông tin sản phẩm</a></li>
                                        </ul>
                                        <div class="css-panes">
                                            <div id="description" class="content" style="margin: 0px; line-height: normal; overflow: auto;">
<?php echo $product->Description_s; ?>
                                </div>
                            </div>
                        </div>

                        <!--content :Gạch ngang-->
                        <hr width="100%" style="color:orange; height:1px; margin-top:10px;"/>

                        <?php
                                global $res;
                                if (isset($_POST['btnSend']) && isset($_POST['commentEmail'])
                                        && isset($_POST['commentContent'])) {
                                    $ProductCode_s = $productID;
                                    $Name_s = $_POST['commentName'];
                                    $Email_s = $_POST['commentEmail'];
                                    $Subject_s = $_POST['commentTitle'];
                                    $Content_s = $_POST['commentContent'];
                                    $Capcha = $_POST['commentCapcha'];

                                    $Security_code = "";
                                    if (isset($_SESSION['security_code'])) {
                                        $Security_code = $_SESSION['security_code'];
                                    }

                                    if (strcmp($Capcha, $Security_code) == 0) {
                                        if ($repository->InsertComment($Name_s, $Email_s, $Subject_s, $Content_s, $ProductCode_s)) {
                                            $res = 1;
                                        } else {
                                            $res = 0;
                                        }
                                    } else {
                                        $res = -1;
                                    }
                                }
                        ?>

                                <!--content :Xem comment-->
                                <div class="viewCmt">
                                    <label id="divViewComment">Xem đánh giá sản phẩm >>>></label>
                                </div>
                                <div id="comments" style="display:none;background-color:#ECEDEF;overflow: scroll; height: 350px;">
                            <?php
                                if (count($commentList) > 0) {
                                    for ($i = 0; $i < count($commentList); $i++) {
                            ?>
                                        <div style="border: 1px solid #4d4b49; padding: 10px;">
                                            <p style="color: #467AA7; font-size: 13px;font-weight: bold; text-align:left;">
<?php echo $commentList[$i]->Subject_s; ?></p>
                                    <p style="font-style: italic; font-weight: normal; font-size: 12px; text-align: justify;">
<?php echo $commentList[$i]->Content_s; ?>
                                    </p>
                                    <p style="color: #7D7D7D; font-style: italic; text-align: right;">
<?php echo $commentList[$i]->Name_s; ?>" - "<?php echo $commentList[$i]->Email_s; ?>"</p>
                                </div>
                            <?php
                                    }
                                } else {
                            ?>
                                    <div style="padding: 10px;">Sản phẩm chưa có bình luận</div>
<?php } ?>
                            </div><!-- content :Xem comment-->

                            <!--content :Viết comment-->
                            <div  id="divSendComment" style="text-align:left;padding-top:10px;background-color: #626262; height:20px;border-top:1px #EFEFEF solid;">
                                <label style="color: #F7F7F7; margin-left: 20px; font-weight: bold;cursor: pointer;">Viết bình luận >>>></label>
                            </div>
                            <div id="sendComment" style="font-size:13px;background-color:#ECEDEF; display: none;overflow:hidden; border:1px #999999 solid;">
                                <p>Điền thông tin và nhận xét của bạn về sản phẩm</p>
                                <table style="width: 90%;">
                                    <tr>
                                        <td class="title">Tiêu đề</td>
                                        <td style="text-align:left;">
                                            <input type="text" name="commentTitle" id="commentTitle" style="width: 236px; margin-left:20px;"/></td>
                                    </tr>
                                    <tr>
                                        <td class="title">Nội dung</td>
                                        <td class="content">
                                            <textarea name="commentContent" id="commentContent" style="width: 451px; height: 190px; margin-left:20px; margin-bottom:10px;" onchange="checkCommentInfo()"></textarea>
                                            <p style="margin:0px;"><label id="alertContent" style="color:red;"></label></p>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="title">Tên</td>
                                        <td class="content">
                                            <input name="commentName" id="commentName" type="text" style="width: 236px; margin-left:20px" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="title">Email</td>
                                        <td class="content">
                                            <input name="commentEmail" id="commentEmail" type="text" style="width: 236px; margin-left:20px;" onchange="checkCommentInfo()" />
                                            <label id="alertEmail" style="color:red;"></label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="title">Mã xác nhận</td>
                                        <td class="content"><img alt="Mã xác nhận" src="../library/random_image.php" style="width: 150px; margin-left:20px; margin-bottom:10px;"/></td>
                                    </tr>
                                    <tr>
                                        <td class="title">Xác nhận</td>
                                        <td class="content" ><input name="commentCapcha" id="commentCapcha" type="text" style="width: 236px; margin-left:20px;" onchange="checkCommentInfo()"/>
                                            <label id="alertConfirm" style="color:red;"></label>
                                        </td>
                                    </tr>
                                    <tr>

                                        <td colspan="2" align="center" id="tdSubmit">
                                            <!--<input class="btn" name="btnSend" id="btnSend" type="submit" value="Gửi bình luận" style="margin-top:10px; color:#cccccc;" disabled="disabled">-->
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><p></p></td>
                                    </tr>
                                </table>

                            </div> <!--content :Viết comment-->
                        </div>


                        <div id="result" style="color:red; text-align:left; font-weight:bold; padding:20px;">
                        <?php
                                if (isset($res)) {
                                    if ($res == 1) {
                                        echo "Bình luận của bạn đã được gửi thành công!";
                                    } else if ($res == 0) {
                                        echo "Có lỗi xảy ra khi gửi bình luận! Vui lòng thử lại sau";
                                    }
                                    else
                                        echo "Mã xác nhận không hợp lệ! Vui lòng thử lại";
                                }
                        ?>
                            </div>

<?php } else { ?>

                                <div style="overflow: hidden;clear: both;margin: 10px 0px 10px 0px;text-align: left;padding-left:30px;">
                                    <p><span style="font-size: 13px;">Sản phẩm này không tồn tại!</span></p>
                                    <p><span style="font-size: 13px;">Vui lòng kiểm tra lại đường liên kết</span></p>
                                </div>
                    <?php } ?>
                    <?php
                        } else {
                    ?>
                            <div style="overflow: hidden;clear: both;margin: 10px 0px 10px 0px;text-align: left;padding-left:30px;">
                                <p><span style="font-size: 13px;">Sản phẩm này không tồn tại!</span></p>
                                <p><span style="font-size: 13px;">Vui lòng kiểm tra lại đường liên kết</span></p>
                            </div>
<?php } ?>
                    </form>

                </div>

            <?php
                        include("../includes/frontend_footer.php");
            ?>
        </div>
    </body>
</html>